package com.wuhuahe.learning.lagou.service.user.controller;

import com.wuhuahe.learning.lagou.service.common.ResponseData;
import com.wuhuahe.learning.lagou.service.user.pojo.RegisterInfo;
import com.wuhuahe.learning.lagou.service.user.pojo.Token;
import com.wuhuahe.learning.lagou.service.user.pojo.User;
import com.wuhuahe.learning.lagou.service.user.service.TokenService;
import com.wuhuahe.learning.lagou.service.user.service.UserService;
import com.wuhuahe.learning.lagou.service.user.utiz.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private TokenService tokenService;

    @PostMapping("/register")
    public ResponseData register(@RequestBody RegisterInfo registerInfo, HttpServletResponse response) {
        boolean register = userService.register(registerInfo);
        if (register) {
            String token = JwtUtil.generateToken(registerInfo.getEmail(), registerInfo.getPassword());
            Token tk = new Token();
            tk.setEmail(registerInfo.getEmail());
            tk.setToken(token);
            try {
                long tokenId = tokenService.insertToken(tk);
                Cookie cookie = new Cookie("sessionId", token);
                response.addCookie(cookie);
                return new ResponseData().success(token);
            } catch (Exception e) {
                return new ResponseData().fail(e.getMessage());
            }
        }
        return new ResponseData().fail("register user failed!");
    }

    @GetMapping("/isRegistered/{email}")
    public boolean isRegistered(@PathVariable("email") String email) {
        return userService.isRegistered(email);
    }

    @PostMapping("/login")
    public ResponseData login(@RequestBody User user, HttpServletResponse response) {
        System.out.println("email=" + user.getEmail() + ",password=" + user.getPassword());
        User u = userService.getUserByEmailAndPassword(user.getEmail(), user.getPassword());
        if (u != null) {

            String token = JwtUtil.generateToken(user.getEmail(), user.getPassword());

            Token tk = tokenService.findByEmail(user.getEmail());
            if (tk != null) {
                tk.setEmail(user.getEmail());
                tk.setToken(token);
                tokenService.updateToken(tk);
            } else {
                tk = new Token();
                tk.setEmail(user.getEmail());
                tk.setToken(token);
                tokenService.insertToken(tk);
            }
            Cookie cookie = new Cookie("sessionId", token);
            response.addCookie(cookie);
            return new ResponseData().success(token);
        } else {
            return new ResponseData().unauthorized();
        }

    }

    @GetMapping("/info/{token}")
    public String info(@PathVariable("token") String token) {
        Map<String, String> userMap = JwtUtil.validateToken(token);

        String email = userMap.get("email");
        String password = userMap.get("password");

        User user = userService.getUserByEmailAndPassword(email, password);
        if (user != null) return email;
        return "";
    }
}
